package org.mtdownload.dialogs;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Toolkit;
import java.awt.datatransfer.DataFlavor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

public class AlternateDownloadUrlsPanel extends JPanel {

	private static final long serialVersionUID = 3617578193255543093L;
	
	private JScrollPane scroll=new JScrollPane();
	private JList list=new JList();
	private DefaultListModel model=new DefaultListModel();
	

	public AlternateDownloadUrlsPanel() {
		super();
		init();
	}
	
	private void init(){
		setLayout(new BorderLayout());
		list.setModel(model);
		scroll.setViewportView(list);
		
		JPanel bPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,20,5));
		JButton addButton=new JButton("Add");		
		addButton.setMnemonic(KeyEvent.VK_A);
		addButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String defaultValue=null;
				try{
					Object obj=Toolkit.getDefaultToolkit().getSystemClipboard().getData(new DataFlavor(String.class,"text/text"));
					if(obj!=null && obj instanceof String){
						defaultValue=(String)obj;
					}
				}catch(Exception exc){/*ignored*/}
				String result=(String)JOptionPane.showInputDialog(list,"URL:","Add an alternate URL",JOptionPane.PLAIN_MESSAGE,null,null,defaultValue);
				if(result!=null){
					model.addElement(result);
				}
			}			
		});
		final JButton editButton=new JButton("Edit");
		editButton.setMnemonic(KeyEvent.VK_E);
		editButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				String originalSelection=(String)list.getSelectedValue();
				if(originalSelection==null)return;
				String result=(String)JOptionPane.showInputDialog(list,"URL:","Edit an alternate URL",JOptionPane.PLAIN_MESSAGE,null,null,originalSelection);
				if(result!=null){
					int index=list.getSelectedIndex();
					if(index<0)return;
					model.setElementAt(result,index);
				}
			}			
		});
		editButton.setEnabled(false);
		final JButton removeButton=new JButton("Remove");
		removeButton.setMnemonic(KeyEvent.VK_R);
		removeButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				Object[] values=list.getSelectedValues();
				if(values==null || values.length<=0)return;				
				for(int i=0;i<values.length;i++){
					model.removeElement(values[i]);
				}
			}			
		});
		removeButton.setEnabled(false);
		list.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
		list.addListSelectionListener(new ListSelectionListener(){
			public void valueChanged(ListSelectionEvent e) {
				editButton.setEnabled(list.getSelectedValue()!=null);
				removeButton.setEnabled(list.getSelectedValue()!=null);
			}
		});
		bPanel.add(addButton);
		bPanel.add(editButton);
		bPanel.add(removeButton);
		
		add(scroll,BorderLayout.CENTER);
		add(bPanel,BorderLayout.SOUTH);
	}
	public List<String> getAlternateUrls(){
		List<String> l=new ArrayList<String>();
		for(int i=0;i<model.size();i++){
			l.add((String)model.elementAt(i));
		}
		return l;
	}	
}
